<template>
  <!-- 缺料要货表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="50%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="工厂id" prop="plantId">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.plantId"
          placeholder="请输入工厂id"
          clearable
        />

        <span v-else>{{ formData.plantId }}</span>
      </el-form-item>
      <el-form-item label="工作中心id" prop="workCenterId">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.workCenterId"
          placeholder="请输入工作中心id"
          clearable
        />

        <span v-else>{{ formData.workCenterId }}</span>
      </el-form-item>
      <el-form-item
        label="要货记录编码;与其他系统交互的唯一任务编号"
        prop="requestCode"
      >
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.requestCode"
          placeholder="请输入要货记录编码;与其他系统交互的唯一任务编号"
          clearable
        />

        <span v-else>{{ formData.requestCode }}</span>
      </el-form-item>
      <el-form-item label="物料编号" prop="materialCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.materialCode"
          placeholder="请输入物料编号"
          clearable
        />

        <span v-else>{{ formData.materialCode }}</span>
      </el-form-item>
      <el-form-item label="要货数量" prop="requestQuantity">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.requestQuantity"
          placeholder="请输入要货数量"
          clearable
        />

        <span v-else>{{ formData.requestQuantity }}</span>
      </el-form-item>
      <el-form-item label="送货物料条码" prop="arriveBarcode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.arriveBarcode"
          placeholder="请输入送货物料条码"
          clearable
        />

        <span v-else>{{ formData.arriveBarcode }}</span>
      </el-form-item>
      <el-form-item label="送达时间" prop="arriveTime">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.arriveTime"
          placeholder="请输入送达时间"
          clearable
        />

        <span v-else>{{ formData.arriveTime }}</span>
      </el-form-item>
      <el-form-item label="送达数量" prop="arriveQuantity">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.arriveQuantity"
          placeholder="请输入送达数量"
          clearable
        />

        <span v-else>{{ formData.arriveQuantity }}</span>
      </el-form-item>
      <el-form-item label="数量单位" prop="quantityUnit">
        <el-select
          v-if="!props.isReadonly"
          v-model="formData.quantityUnit"
          placeholder="请选择数量单位"
          clearable
        >
          <el-option
            v-for="item in dictionaryStore.filterDictList('MeasurementUnit')"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>

        <span v-else>{{
          dictionaryStore.filterDictName(
            "MeasurementUnit",
            formData.quantityUnit
          )
        }}</span>
      </el-form-item>
      <el-form-item label="物料批次" prop="materialBatch">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.materialBatch"
          placeholder="请输入物料批次"
          clearable
        />

        <span v-else>{{ formData.materialBatch }}</span>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MESMESMATERIALREQUESTADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  plantId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  workCenterId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  requestCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  materialCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  requestQuantity: [],
  arriveBarcode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  arriveTime: [],
  arriveQuantity: [],
  quantityUnit: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  materialBatch: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  organizationId: "",
  organizationCode: "",
  plantId: "",
  plantCode: "",
  workCenterId: "",
  workCenterCode: "",
  requestCode: "",
  materialId: "",
  materialCode: "",
  requestQuantity: "",
  arriveBarcode: "",
  arriveTime: "",
  arriveQuantity: "",
  quantityUnit: "",
  isArrive: "",
  isReceive: "",
  receiveUser: "",
  receiveTime: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
  materialBatch: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        organizationId,
        organizationCode,
        plantId,
        plantCode,
        workCenterId,
        workCenterCode,
        requestCode,
        materialId,
        materialCode,
        requestQuantity,
        arriveBarcode,
        arriveTime,
        arriveQuantity,
        quantityUnit,
        isArrive,
        isReceive,
        receiveUser,
        receiveTime,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
        materialBatch,
      } = newVal;
      Object.assign(formData, {
        id,
        organizationId,
        organizationCode,
        plantId,
        plantCode,
        workCenterId,
        workCenterCode,
        requestCode,
        materialId,
        materialCode,
        requestQuantity,
        arriveBarcode,
        arriveTime,
        arriveQuantity,
        quantityUnit,
        isArrive,
        isReceive,
        receiveUser,
        receiveTime,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
        materialBatch,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.organizationId = "";
  formData.organizationCode = "";
  formData.plantId = "";
  formData.plantCode = "";
  formData.workCenterId = "";
  formData.workCenterCode = "";
  formData.requestCode = "";
  formData.materialId = "";
  formData.materialCode = "";
  formData.requestQuantity = "";
  formData.arriveBarcode = "";
  formData.arriveTime = "";
  formData.arriveQuantity = "";
  formData.quantityUnit = "";
  formData.isArrive = "";
  formData.isReceive = "";
  formData.receiveUser = "";
  formData.receiveTime = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
  formData.materialBatch = "";
}

const loading = ref(false);

/** 缺料要货保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mes/mesmaterialrequest/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
